<?php

namespace app\admin\service;

use app\common\model\commonModel;
use app\common\model\trResource;
use app\common\util\ErrorCode;

/**
 * Class Auth
 * @package app\admin\service
 * 线路菜单服务类
 */
class LineMenuService
{
    private $returnData;
    private $field = [
        "title" => "require", "icon" => "require", "url" => "require", "image_size" => "", "sort" => 0
    ]; //填入数据库需要存储的表字段
    private $data = [];
    private $mall_id = 0;
    private $params;

    public function __construct($data, $params = [])
    {
        $this->returnData = $data;
        $this->params = $params;
        $this->mall_id = request()->mallData["id"];
    }

    //分页列表
    public function getList()
    {
        $page = isset($this->params['page']) ? $this->params['page'] : 1;
        $pageSize = isset($this->params['pageSize']) ? $this->params['pageSize'] : 10;
        //搜索条件
        $filter = array(["mall_id", "=", $this->mall_id]);
        if(!empty($this->params['search'])){
            $filter[] = ['title', 'like', $this->params["search"] . "%"];
        }
        if(!empty($this->params['type'])){
            $filter[] = ['type', '=', $this->params['type']];
        }
        $data = commonModel::getList(lineMenuTable, "*", $filter, $page, $pageSize, "sort desc,id asc");
        if(!$data["data"]){
            $this->returnData['data'] = ['list' => [], 'totalPage' => $page, 'currentPage' => $page, 'count' => 0];
        }else {
            foreach ($data["data"] as $k => $item){
                if(isset($item["icon"])){
                    $data["data"][$k]["iconUrl"] = tome_dia(trResource::getValue("url", ["id" => $item["icon"]]));
                }
            }
            $this->returnData['data'] = ['list' => $data['data'], 'totalPage' => $data['last_page'], 'currentPage' => $data['current_page'], 'count' => $data['total']];
        }
        return $this->returnData;
    }

    //信息
    public function getDetail(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        $data = commonModel::getFields(lineMenuTable, "*", ["id" => $this->params['id'], "mall_id" => $this->mall_id], true, false);
        if(isset($data["icon"])){
            $data["iconUrl"] = tome_dia(trResource::getValue("url", ["id" => $data["icon"]]));
        }
        $this->returnData['data'] = $data;
        return $this->returnData;
    }

    //添加
    public function toAdd(){
        foreach ($this->field as $k => $v) {
            $ks = parse_name($k); //转换字段，变驼峰
            if ($v === 'require' && !isset($this->params[$ks])) {
                return ErrorCode::code("params_not_exist", $ks);
            }
            $this->data[$k] = isset($this->params[$ks]) ? $this->params[$ks] : $v;
        }
        $this->data["mall_id"] = $this->mall_id;
        $imageData = getimagesize(tome_dia(trResource::getValue("url", ["id" => $this->data["icon"]])));
        $this->data["image_size"] = "{$imageData[0]}px * {$imageData[1]}px";
        $this->data["create_time"] = date("Y-m-d H:i");
        $result = commonModel::toAdd(lineMenuTable, $this->data);
        if(isset($result["code"])){
            return $result;
        }
        if($result) {
            return $this->returnData;
        }else{
            $this->returnData = ErrorCode::code("fail");
            $this->returnData["msg"] = "添加失败";
            return $this->returnData;
        }
    }

    //更新
    public function toUpdate(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        foreach ($this->field as $k => $v) {
            $ks = parse_name($k); //转换字段，变驼峰
            if ($v === 'require' && !isset($this->params[$ks])) {
                return ErrorCode::code("params_not_exist", $ks);
            }
            if(isset($this->params[$ks]))
                $this->data[$k] = $this->params[$ks];
        }
        if(isset($this->data["icon"])) {
            $imageData = getimagesize(tome_dia(trResource::getValue("url", ["id" => $this->data["icon"]])));
            $this->data["image_size"] = "{$imageData[0]}px * {$imageData[1]}px";
        }
        $result = commonModel::toUpdate(lineMenuTable, $this->data, ['id' => $this->params['id'], "mall_id" => $this->mall_id]);
        if(isset($result["code"])){
            return $result;
        }
        if($result) {
            return $this->returnData;
        }else{
            $this->returnData = ErrorCode::code("fail");
            $this->returnData["msg"] = "更新失败";
            return $this->returnData;
        }
    }

    //更新状态
    public function toUpdateStatus(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        if(!isset($this->params['status'])){
            return ErrorCode::code("params_not_exist", "status");
        }
        if(!in_array($this->params["status"], [0,1])){ // 状态：1开启；0关闭
            return ErrorCode::code("params_error", "status");
        }
        $result = commonModel::toUpdate(lineMenuTable, ["status" => $this->params["status"]], ['id' => $this->params['id'], "mall_id" => $this->mall_id]);
        if(isset($result["code"])){
            return $result;
        }
        if($result) {
            return $this->returnData;
        }else{
            $this->returnData = ErrorCode::code("fail");
            $this->returnData["msg"] = "更新失败";
            return $this->returnData;
        }
    }

    //删除
    public function toDel(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        $result = commonModel::delById(lineMenuTable, $this->params['id']);
        if (!$result) {
            $this->returnData = ErrorCode::code("fail");
            $this->returnData["msg"] = "删除失败";
        }
        return $this->returnData;
    }
}